package com.ahdy.controller;

import com.ahdy.service.BaiduOcrService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/ocr")
public class OcrController {

    private BaiduOcrService ocrService;

    public OcrController(BaiduOcrService ocrService) {
        this.ocrService = ocrService;
    }

    /**
     * 标准文字识别接口（文件上传）
     * POST /api/ocr/basic
     */
    @PostMapping("/basic")
    public ResponseEntity<Map<String, Object>> basicOcr(
            @RequestParam("file") MultipartFile file,
            @RequestParam(required = false, defaultValue = "CHN_ENG") String languageType) {

        Map<String, Object> response = new HashMap<>();

        try {
            String result = ocrService.basicRecognize(file);
            response.put("success", true);
            response.put("data", result);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 标准文字识别接口（图片路径）
     * POST /api/ocr/basic/path
     */
    @PostMapping("/basic/path")
    public ResponseEntity<Map<String, Object>> basicOcrByPath(
            @RequestParam("imagePath") String imagePath,
            @RequestParam(required = false, defaultValue = "CHN_ENG") String languageType) {

        Map<String, Object> response = new HashMap<>();

        try {
            String result = ocrService.basicRecognize(imagePath);
            response.put("success", true);
            response.put("data", result);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 标准文字识别接口（通用接口，支持文件和路径）
     * POST /api/ocr/basic/universal
     */
    @PostMapping("/basic/universal")
    public ResponseEntity<Map<String, Object>> basicOcrUniversal(
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(value = "imagePath", required = false) String imagePath,
            @RequestParam(required = false, defaultValue = "CHN_ENG") String languageType) {

        Map<String, Object> response = new HashMap<>();

        try {
            String result;

            // 优先处理文件上传
            if (file != null && !file.isEmpty()) {
                result = ocrService.basicRecognize(file);
            }
            // 其次处理图片路径
            else if (imagePath != null && !imagePath.trim().isEmpty()) {
                result = ocrService.basicRecognize(imagePath);
            }
            // 都没有提供时报错
            else {
                response.put("success", false);
                response.put("message", "请提供文件或图片路径");
                return ResponseEntity.badRequest().body(response);
            }

            response.put("success", true);
            response.put("data", result);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 高精度文字识别接口（文件上传）
     * POST /api/ocr/accurate
     */
    @PostMapping("/accurate")
    public ResponseEntity<Map<String, Object>> accurateOcr(
            @RequestParam("file") MultipartFile file,
            @RequestParam(required = false, defaultValue = "CHN_ENG") String languageType) {

        Map<String, Object> response = new HashMap<>();

        try {
            String result = ocrService.accurateRecognize(file);
            response.put("success", true);
            response.put("data", result);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 高精度文字识别接口（图片路径）
     * POST /api/ocr/accurate/path
     */
    @PostMapping("/accurate/path")
    public ResponseEntity<Map<String, Object>> accurateOcrByPath(
            @RequestParam("imagePath") String imagePath,
            @RequestParam(required = false, defaultValue = "CHN_ENG") String languageType) {

        Map<String, Object> response = new HashMap<>();

        try {
            String result = ocrService.accurateRecognize(imagePath);
            response.put("success", true);
            response.put("data", result);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 高精度文字识别接口（通用接口，支持文件和路径）
     * POST /api/ocr/accurate/universal
     */
    @PostMapping("/accurate/universal")
    public ResponseEntity<Map<String, Object>> accurateOcrUniversal(
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(value = "imagePath", required = false) String imagePath,
            @RequestParam(required = false, defaultValue = "CHN_ENG") String languageType) {

        Map<String, Object> response = new HashMap<>();

        try {
            String result;

            // 优先处理文件上传
            if (file != null && !file.isEmpty()) {
                result = ocrService.accurateRecognize(file);
            }
            // 其次处理图片路径
            else if (imagePath != null && !imagePath.trim().isEmpty()) {
                result = ocrService.accurateRecognize(imagePath);
            }
            // 都没有提供时报错
            else {
                response.put("success", false);
                response.put("message", "请提供文件或图片路径");
                return ResponseEntity.badRequest().body(response);
            }

            response.put("success", true);
            response.put("data", result);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 异常处理
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<Map<String, Object>> handleIllegalArgument(IllegalArgumentException e) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("message", e.getMessage());
        return ResponseEntity.badRequest().body(response);
    }
}
